﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using LabMS.BLL;
using LabMS.Model;
using ExtendBLL;

namespace LabMS.Web.LabPersonnel
{
    public partial class ListResponsibility : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("RYGWZZGL");
            strPageUserType.Add("mana");
            //checkall.Attributes.Add("onclick", "checkAllChanged(this, 'gvResponsList')");
            if(!IsPostBack)
            {
                 BindGridViewList("");
                 DeleteBtn.Attributes.Add("onclick", "javascript:return confirm('你确认要删除这些列吗?')");
            }          
        }

        //为GridView绑定数据
        private void BindGridViewList(string strWhere)
        {
            ExtendBLL.Responsibility responsibility = new ExtendBLL.Responsibility();
            List<ExtendModule.ResponsibilityAndOther> responsibilityList = new List<ExtendModule.ResponsibilityAndOther>();

            responsibilityList = responsibility.GetResponsList(strWhere);
            gvResponsList.DataSource = responsibilityList;
            gvResponsList.DataBind();
        }

        protected void GridViewRespons_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断是否为数据行
            if(e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标移动时改变背景色
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='6699FF';this.style.color='8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
                //鼠标点击数据行时的链接
                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    e.Row.Cells[i].Attributes.Add("onDblclick", "window.location.href='ResponsibilityPreview.aspx?ID="
                        + gvResponsList.DataKeys[gvResponsList.DataKeys.Count-1].Value.ToString() + "'");
                }
            }
        }

        //删除选择的岗位职责列
        protected void Delete_OnClick(object sender, EventArgs e)
        {
            CheckBox cb;
            LabMS.BLL.Responsibility responsibility = new LabMS.BLL.Responsibility();
            try
            {
                for (int i = 0; i < gvResponsList.Rows.Count; i++ )
                {
                    cb = (CheckBox)gvResponsList.Rows[i].FindControl("ResponsCB");
                    if (cb.Checked)
                    {
                        string RID = gvResponsList.DataKeys[i].Value.ToString();
                        responsibility.Delete(int.Parse(RID));
                    }
                }
                if (tbResKeyWord.Text.Trim().Equals(""))
                {
                    BindGridViewList("");
                }
                else
                {
                    BindGridViewList(SearchString());
                }
                LabMS.Common.JShelper.JSAlert(Page, "deleteSuccess", "删除成功!");
            }
            catch(Exception ex)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "删除失败!");
            }
        }

        //搜索查询语句
        protected string SearchString()
        {
            string strWhere = "";
            if (ddlResKeyColum.SelectedValue.Equals("Resp_Name"))
            {
                strWhere = " Resp_Name like '%" + tbResKeyWord.Text.Trim() + "%'";
            }
            else if (ddlResKeyColum.SelectedValue.Equals("Resp_Content"))
            {
                strWhere = " Resp_Content like '%" + tbResKeyWord.Text.Trim() + "%'";
            }
            else if (ddlResKeyColum.SelectedValue.Equals("Resp_Lab"))
            {
                strWhere = " Lab_Name like '%" + tbResKeyWord.Text.Trim() + "%'";
            }

            return strWhere;
        }
        //搜索
        protected void btSearch_Click(object sender, EventArgs e)
        {
            BindGridViewList(SearchString());
        }

        //分页
        protected void GridViewRespons_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvResponsList.PageIndex = e.NewPageIndex;
            if (tbResKeyWord.Text.Trim().Equals(""))
            {
                BindGridViewList("");
            }
            else
            {
                BindGridViewList(SearchString());                
            }
        }
    }
}
